package bb.lanxing.model.json.club;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import bb.lanxing.model.json.UserAvatarMedal;


public class ClubNewsDeserializer implements JsonDeserializer<ClubNews> {
    @Override
    public ClubNews deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
        JsonObject asJsonObject = jsonElement.getAsJsonObject();
        ClubNews clubNews = new ClubNews();
        clubNews.userId = (Long) get(asJsonObject, "user_id", 0L);
        clubNews.eventId = (Long) get(asJsonObject, "event_id", 0L);
        clubNews.userName = (String) get(asJsonObject, "user_name", "");
        clubNews.userPicUrl = (String) get(asJsonObject, "user_pic_url", "");
        clubNews.picUrl = (String) get(asJsonObject, "pic_url", "");
        clubNews.teamId = (Long) get(asJsonObject, "team_id", 0L);
        clubNews.eventName = (String) get(asJsonObject, "event_title", "");
        clubNews.eventPic = (String) get(asJsonObject, "event_pic", "");
        clubNews.time = (Long) get(asJsonObject, "time", 0L);
        clubNews.type = (Integer) get(asJsonObject, "type", 0);
        clubNews.id = (Long) get(asJsonObject, "id", 0L);
        clubNews.likerCount = (Integer) get(asJsonObject, "xing_count", 0);
        clubNews.isLiked = (Integer) get(asJsonObject, "is_xing", 0) != 0;
        clubNews.top = (Integer) get(asJsonObject, "top", 0) != 0;
        clubNews.feedLevel = (Integer) get(asJsonObject, "feed_level", 2);
        clubNews.userAvatarMedals =  jsonDeserializationContext.deserialize(asJsonObject.getAsJsonArray("medal_small"), new TypeToken<List<UserAvatarMedal>>() {
        }.getType());
        clubNews.plateNum = (String) get(asJsonObject, "license_number", "");
        clubNews.proName = (String) get(asJsonObject, "pro_name", "");
        clubNews.userLevel = (Integer) get(asJsonObject, "ulevel", 1);
        clubNews.likerList =  jsonDeserializationContext.deserialize(asJsonObject.getAsJsonArray("xings"), new TypeToken<List<NewsLiker>>() {
        }.getType());
        clubNews.comment =  jsonDeserializationContext.deserialize(asJsonObject.getAsJsonArray("comment"), new TypeToken<List<NewsComment>>() {
        }.getType());
        JsonElement jsonElement2 = asJsonObject.get((clubNews.type == 1 || clubNews.type == 0) ? "content" : "feed_info");
        clubNews.content = jsonElement2 != null ? decodeContent(clubNews.type, jsonElement2, jsonDeserializationContext) : null;
        return clubNews;
    }

    private <T> T get(JsonObject jsonObject, String str, T t) {
        JsonElement jsonElement = jsonObject.get(str);
        if (jsonElement != null) {
            Class<?> cls = t.getClass();
            if (cls == Integer.TYPE || cls == Integer.class) {
                return (T) Integer.valueOf(jsonElement.getAsInt());
            }
            if (cls == Long.TYPE || cls == Long.class) {
                return (T) Long.valueOf(jsonElement.getAsLong());
            }
            if (cls == String.class) {
                return (T) jsonElement.getAsString();
            }
        }
        return t;
    }

    private NewsContent decodeContent(int i, JsonElement jsonElement, JsonDeserializationContext jsonDeserializationContext) {
        if (i == 0 || i == 1) {
            return new NewsText(jsonElement.getAsString());
        }
        if (i != 2) {
            if (i != 3) {
                if (i != 4) {
                    if (i != 5) {
                        if (i != 7) {
                            if (i != 9) {
                                return null;
                            }
                            return (NewsContent) jsonDeserializationContext.deserialize(jsonElement, NewsJoinWebActivity.class);
                        }
                        return (NewsContent) jsonDeserializationContext.deserialize(jsonElement, NewsMonthly.class);
                    }
                    return (NewsContent) jsonDeserializationContext.deserialize(jsonElement, NewsMatch.class);
                }
                return (NewsContent) jsonDeserializationContext.deserialize(jsonElement, NewsSeg.class);
            }
            return (NewsContent) jsonDeserializationContext.deserialize(jsonElement, NewsMedal.class);
        }
        return (NewsContent) jsonDeserializationContext.deserialize(jsonElement, NewsJoinActivity.class);
    }
}
